package com.itbaizhan.shopping_search_service.listen;

import com.itbaizhan.shopping_common.pojo.DomainEvent;
import com.itbaizhan.shopping_common.pojo.GoodsDesc;
import com.itbaizhan.shopping_common.service.SearchService;
import org.apache.dubbo.config.annotation.DubboReference;
import org.apache.rocketmq.spring.annotation.ConsumeMode;
import org.apache.rocketmq.spring.annotation.RocketMQMessageListener;
import org.apache.rocketmq.spring.core.RocketMQListener;

@RocketMQMessageListener(
        topic = "goods_event_topic",
        consumeMode = ConsumeMode.ORDERLY, //集群消费+有序
        consumerGroup = "search_search_group"
)
public class SearchServiceListener implements RocketMQListener<Object> {
    @DubboReference
    private SearchService searchService;


    @Override
    public void onMessage(Object o) {
        DomainEvent event = (DomainEvent) o;
        // 判断消息类型
        if(event.getEventType().equals("sync_goods")) { //上架消息 ||更新
            searchService.syncGoodsToEs((GoodsDesc) event.getData());
        }else if(event.getEventType().equals("off_goods")) { //下架商品
            System.out.println("删除商品索引");
            searchService.delete((Long) event.getData());
        }
    }

}
